﻿using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;
using Page = Cms.Domain.Page;

namespace Cms.Web.UI.Admin
{
    public partial class ApplicationPages : AdminPage, IApplicationPagesView
    {
        private ApplicationPagesPresenter _presenter;

        #region IApplicationPagesView Members

        public int ApplicationID
        {
            get
            {
                if (ViewState["ApplicationID"] == null)
                    ViewState["ApplicationID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["ApplicationID"];
            }
        }

        public void SetPages(IList<ApplicationPage> pages)
        {
            rpPages.DataSource = pages;
            rpPages.DataBind();
            pnlNoPages.Visible = rpPages.Items.Count == 0;
        }

        public void SetHeading(string applicationName)
        {
            HeadingText = GetText(this, "Heading") + " &mdash; " + applicationName;
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/ApplicationPages.xml"; }
        }

        public void DisplayCreateMasterTemplateAlert()
        {
            hlNewPage.Attributes.Add(
                "onclick",
                "cms.Dialogs.Alert(null, '" +
                GetText(this, "CreateTemplateAlertHeader") + "', '" +
                GetText(this, "CreateMasterTemplateAlertMessage") + "', '" +
                GetText(this, "CreateTemplateAlertOK") +
                "'); return false;");
        }

        public void DisplayCreateDetailTemplateAlert()
        {
            hlNewPage.Attributes.Add(
                "onclick",
                "cms.Dialogs.Alert(null, '" +
                GetText(this, "CreateTemplateAlertHeader") + "', '" +
                GetText(this, "CreateDetailTemplateAlertMessage") + "', '" +
                GetText(this, "CreateTemplateAlertOK") +
                "'); return false;");
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new ApplicationPagesPresenter(this);
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
                hlBack.NavigateUrl = "Admin/Applications.aspx";
                hlNewPage.NavigateUrl = "Admin/EditApplicationPage.aspx?ApplicationID=" + ApplicationID;
            }

            lbDeletePage.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                         "function (status) { " +
                                         "if (status == 'ok') " +
                                         "cms.Lists.ShowPaneOverlay('pagePaneOverlay');" +
                                         "else " +
                                         "cms.Lists.HidePaneOverlays(); " +
                                         "}, " +
                                         "'" + GetText(this, "DeleteHeader") + "', " +
                                         "'" + GetText(this, "DeleteMessage") + "', " +
                                         "'" + GetText(this, "DeleteOK") + "', " +
                                         "'" + GetText(this, "DeleteCancel") + "', " +
                                         "true);";
        }

        protected void rpPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var p = (Page) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litFilename = (Literal) e.Item.FindControl("litFilename");
            var imgPage = (HtmlImage) e.Item.FindControl("imgPage");
            litID.Text = "<span id=\"A" + p.ID + "\">";
            litName.Text = p.Name;
            litFilename.Text = p.GetVirtualPath();
            imgPage.Src = "Admin/Img/icons/page_code.png";
        }

        protected void lbDeletePage_Click(object sender, EventArgs e)
        {
            _presenter.DeletePage(int.Parse(hidSelectedPageID.Value.Substring(1)));
            UpdatePagePane();
        }

        protected void lbPreviewPage_Click(object sender, EventArgs e)
        {
            string url = _presenter.GetPagePreviewUrl(int.Parse(hidSelectedPageID.Value.Substring(1)));
            Response.Redirect(url);
        }

        private void UpdatePagePane()
        {
            upnlPages.Update();
            ClientScriptProxy.Current.RegisterStartupScript(
                pnlPageActions,
                GetType(),
                "initPageList",
                "initPageList();",
                true);
        }
    }
}